<?php

/**
 * Provincia actions.
 *
 * @package    fiei
 * @subpackage Provincia
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class AulaActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeList(sfWebRequest $request)
  {
   ini_set('max_execution_time', 600); 
   $json   = new Services_JSON(); 
   $examp  = $request->getPostParameter('q');
   $page   = $request->getPostParameter('page'); 
   $limit  = $request->getPostParameter('rows'); 
   $sidx   = $request->getPostParameter('sidx'); 
   $sord   = $request->getPostParameter('sord'); 
   if($request->getParameter('codigo'))
   
   $codigo = $request->getParameter('codigo');
   else
   $codigo = "";
   if($request->getParameter('nombre')) 
     $nombre = $request->getParameter('nombre');
   else
   $nombre = ""; 
   if(!$sidx) 
     $sidx = 1;
   $q  = Doctrine_Query::create()->select('*')->from('Provincia a');
	
   if($codigo!='') 
     $q->addWhere(sprintf('a.id = %s', $codigo)); 
   if($nombre!='')
     $q->addWhere(sprintf('LOWER(a.nombre) LIKE "%%%s%%"', $nombre));  
   $count = $q->execute()->count();
    
   if( $count >0 ) 
     { $total_pages = ceil($count/$limit); } 
   else { $total_pages = 0; } 
   
   if ($page > $total_pages) 
     $page=$total_pages; 
   
   if ($limit<0) 
     $limit = 0; 
   $start = $limit*$page - $limit; 

   if ($start<0) 
     $start = 0;
     
   //if($sidx == 'departamento')
     //$sidx = 'departamento_id';
   $q->orderBy($sidx.' '.$sord)
     ->limit($start.','.$limit);
     
   $results             = $q->execute();
   
   $response['page']    = $page; 
   $response['total']   = $total_pages; 
   $response['records'] = $count;
   $i                   = 0;
   foreach($results as $key => $result) 
   { 
     $response['rows'][$key]['id']   =  $result->getId(); 
     $response['rows'][$key]['cell'] = array($result->getId(),$result->getNombre()); 
   } 
   return $this->renderText($json->encode($response));    
  }
}